package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.PageResult;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.ThreadLocalUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    //环信用户ID查询用户信息:
    public UserInfoVo queryInfoByHuanXin(String huanxinId) {

        //1.先根据环信id查询用户
        User user =userApi.queryUserByHuanXin(huanxinId);
        //2.从用户中得到用户id，查询用户详情
        UserInfo userInfo = userInfoApi.selectUserInfo(user.getId());
        //3.构造vo返回

        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        if(userInfo.getAge() != null) {
            userInfoVo.setAge(userInfo.getAge().toString());
        }

        return userInfoVo;
    }



    /**
     *添加联系人
     * @param friendId
     */
    public void addFriend(Long friendId) {
        //1.把好友关系注册到环信
        Long userId = ThreadLocalUtils.getUserId();
        //俩个参数，一个是当前用户的环信id，一个是对方账号的环信id
        Boolean result = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userId, Constants.HX_USER_PREFIX + friendId);
        if(!result){
            throw new BusinessException(ErrorResult.error());
        }
        //2.把好友关系数据保存到自己的数据库
        friendApi.save(userId,friendId);
    }


    /**
     * 分页查询练习人
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResult queryFriends(Integer page, Integer pageSize, String keyword) {
        //1.分页查询当前用户的好友集合
        Long currentId = ThreadLocalUtils.getUserId();
        List<Friend> friends = friendApi.queryFriends(currentId,page,pageSize);

        //2.判断当前用户是否有好友,没有就new一个PageResult返回
        if(CollUtil.isEmpty(friends)){
           return new PageResult();
        }

        //3.批量获取好友的id，再根据id查询好友的详情
        List<Long> friendIds = CollUtil.getFieldValues(friends, "friendId", Long.class);
        //添加查询的条件，根据关键字查询
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> userInfoMap = userInfoApi.batchQueryUserInfo(friendIds, userInfo);


        //4.遍历好友集合，没遍历一个好友就构造一个vo对象
        List<ContactVo> contactVos = new ArrayList<>();
        for (Friend friend : friends) {

            Long friendId = friend.getFriendId();
            UserInfo userInfo1 = userInfoMap.get(friendId);

            if(userInfo1 != null){
                ContactVo contactVo = ContactVo.init(userInfo1);
                contactVos.add(contactVo);
            }

        }

        //5.构造返回值
        PageResult pageResult = new PageResult(page,pageSize,0,contactVos);
        return pageResult;

    }
}
